<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script>
        function isFunction(fn) {
            return typeof fn !== 'function'
        }

        function maps(callback) {
            //这里就第一个处理了函数判断,其实其他的也应该实现容错处理
            if (this.isFunction(callback)) return []
            const arr = []
            for (let i = 0; i < this.length; i++) {
                arr.push(callback(this[i], i))
            }

            return arr
        }

        function filters(callback) {
            const arr = []
            for (let i = 0; i < this.length; i++) {
                callback(this[i], i) && arr.push(this[i])
            }
            return arr
        }

        function foreach(callback) {
            for (let i = 0; i < this.length; i++) {
                callback(this[i], i)
            }
        }

        function somes(callback) {
            for (let i = 0; i < this.length; i++) {
                console.log(arr[i])
                if (callback(this[i], i)) {
                    return true
                }
            }
            return false
        }

        function reduces(callback, init) {
            //也可以用剩余参数  ...arg
            let pre = arguments.length === 1 ? this[0] : init
            for (let i = 0; i < this.length; i++) {
                pre = callback(pre, this[i], i)
            }
            return pre
        }

        function finds(callback) {
            for (let i = 0; i < this.length; i++) {
                if (callback(this[i], i)) {
                    return arr[i]
                }
            }
            return void 0
        }

        function everys(callback) {
            for (let i = 0; i < this.length; i++) {
                if (!callback(this[i], i)) {
                    return false
                }
            }
            return true
        }

        function findIndexs(callback) {
            for (let i = 0; i < this.length; i++) {
                if (callback(this[i], i)) {
                    return i
                }
            }
            return -1
        }

        const arrayMethod = {
            isFunction,
            maps,
            foreach,
            somes,
            reduces,
            finds,
            everys,
            filters,
        }
        Object.keys(arrayMethod).forEach((key) => {
            Array.prototype[key] = arrayMethod[key]
        })
        const arr = [3, 6, 9]
        // console.log(arr.map(() => {
        // }))
        console.log(arr.maps((item, index) => {
            return {
                num: item
            }
        }), "map")
        console.log('--------------')
        arr.foreach(((item, index) => {
            console.log(item, index)
        }))
        console.log('--------------')
        console.log(arr.reduces((pre, item) => {
            if (item === 3) {
                pre += item
            }
            console.log(pre)
            return pre
        }, 0), 'reduce')
        console.log('--------------')
        console.log(arr.somes(item => item === 3), "some")
        console.log(arr.finds(item => item === 3), "find")
        console.log(arr.everys(item => typeof item === 'number'), "every")
        console.log(arr.filters(item => item === 3),"filter")
    </script>
</head>
<body>

</body>
</html>
